package com.nbjtjc.safe.service.safe;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.safe.RiskMarkItemMapper;
import com.nbjtjc.safe.mapper.safe.RiskMarkMapper;
import com.nbjtjc.safe.model.safe.RiskMark;
import com.nbjtjc.safe.model.safe.RiskMarkItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RiskMarkItemServiceImpl {

    @Autowired
    private RiskMarkItemMapper riskMarkItemMapper;

    @Autowired
    private RiskMarkMapper riskMarkMapper;

    public Result add(RiskMarkItem riskMarkItem){
        Result result=new Result();
        if(ObjectUtil.isNull(checkExist(riskMarkItem))){
            return Result.res();
        }
        RiskMark riskMark=riskMarkMapper.selectByPrimaryKey(riskMarkItem.getMarkId());
        riskMarkItem.setIconUrl(riskMark.getFileUrl());
        riskMarkItemMapper.insert(riskMarkItem);
        result.setData(riskMarkItem);
        return result;
    }

    public RiskMarkItem checkExist(RiskMarkItem riskMarkItem){
        List<RiskMarkItem> riskMarkItems= riskMarkItemMapper.select(riskMarkItem);
        if(CollectionUtil.isEmpty(riskMarkItems)){
            return riskMarkItem;
        }
        return null;
    }
    public Result delete(RiskMarkItem riskMarkItem){
        Result result=new Result();
        riskMarkItemMapper.deleteByPrimaryKey(riskMarkItem.getId());
        return result;
    }
    public Result find(RiskMarkItem riskMarkItem){
        Result result=new Result();
        List<RiskMarkItem> markItemList= riskMarkItemMapper.select(riskMarkItem);
        result.setData(markItemList);
        return result;
    }
}
